package edu.cug.water.crawler.job.task.report;

import edu.cug.water.crawler.common.exception.RRException;
import edu.cug.water.crawler.job.service.ScheduleJobLogService;
import edu.cug.water.crawler.job.task.ITask;
import edu.cug.water.crawler.mail.entity.MailVo;
import edu.cug.water.crawler.mail.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

@Component("job:CrawlerReport")
@Slf4j
public class CrawlerReport implements ITask {

    @Autowired
    ScheduleJobLogService scheduleJobLogService;

    @Autowired
    MailService mailService;

    @Override
    public int run(String params) throws IOException {
        if (params.isEmpty())
            throw new RRException("CrawlerReport: run() param missing!");
        params = params.trim().toLowerCase(Locale.ROOT);

        Map map = null;
        if ("week".equals(params)) {
            map = scheduleJobLogService.statisticsWeekly();
        } else if ("day".equals(params)) {
            map = scheduleJobLogService.statisticsDay();
        } else if ("month".equals(params)) {
            map = scheduleJobLogService.statisticsMonth();
        } else {
            throw new RRException("CrawlerReport: run() parameter is incorrect!");
        }
        MailVo mailVo = new MailVo();




        mailVo.setFrom("1312600368@qq.com").setTo("554417388@qq.com").setSubject("【Crawler " + params + " Report】")
                        .setText(map.toString());

        mailService.sendMail(mailVo);
        return 0;
    }



}
